﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace NetService.Define
{
     public class IPData
     {
          //IP 名称
          private string name = String.Empty;
          public string Name
          {
              get 
              {
                  return name;
              }
              set 
              {
                  name = value;
              }
          }

          //循环访问获得的IP地址
          private List<DNSAddressData> dnsAddressDataList = new List<DNSAddressData>();
          public List<DNSAddressData> DNSAddressDataList 
          {
              get
              {
                  return dnsAddressDataList;
              }
              set
              {
                  dnsAddressDataList = value;
              }
          }

          // 获取本机名 
          private string hostName = String.Empty;
          public string HostName
          {
              get
              {
                  return hostName;
              }
              set
              {
                  hostName = value;
              }
          }
         
          // 根据指定的主机名获取DNS信息
          private string dnsHostByName = String.Empty;
          public string DNSHostByName
          {
              get
              {
                  return dnsHostByName;
              }
              set
              {
                  dnsHostByName = value;
              }
          }

          /**
           * 判断是否为正确的IP地址
           *
           *<param name="strIPadd">需要判断的字符串</param>
           * <returns>true = 是 false = 否</returns>
           */
          public static bool IsRightIP(string strIPadd)
          {
              //利用正则表达式判断字符串是否符合IPv4格式
              if (Regex.IsMatch(strIPadd, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
              {
                  //根据小数点分拆字符串
                  string[] ips = strIPadd.Split('.');
                  if (ips.Length == 4 || ips.Length == 6)
                  {
                      //如果符合IPv4规则
                      if (System.Int32.Parse(ips[0]) < 256 && System.Int32.Parse(ips[1]) < 256 & System.Int32.Parse(ips[2]) < 256 & System.Int32.Parse(ips[3]) < 256)
                          //正确
                          return true;
                      //如果不符合
                      else
                          //错误
                          return false;
                  }
                  else
                      //错误
                      return false;
              }
              else
                  //错误
                  return false;
          }
     }

    public class DNSAddressData 
    {

          //循环访问获得的IP地址
          private string ipAddress = String.Empty;
          public string IPAddress
          {
              get
              {
                  return ipAddress;
              }
              set
              {
                  ipAddress = value;
              }
          }

          // 子网掩码地址
          private string subnetMaskAddress = "255.255.255.0";
          public string SubNetMaskAddress
          {
              get
              {
                  return subnetMaskAddress;
              }
              set
              {
                  subnetMaskAddress = value;
              }
          }

          // 网关IP地址
          private string gateWayAddress = String.Empty;
          public string GateWayAddress
          {
              get
              {
                  return gateWayAddress;
              }
              set
              {
                  gateWayAddress = value;
              }
          }
     }

}
